#!/usr/bin/env python

import string

class CryptManager():
	# According to algorithm described by:
	# http://www.teamfair.info/DC-Protocol.htm#AppendixA.html
	def lockToKey(self, lock):
		llen = len(lock)
		# Hopefull this is somewhat efficient
		key = "".join([chr(ord(lock[idx]) ^ ord(lock[idx - 1]))
														for idx in range(1, llen)])

		# Append first key
		key = chr(ord(lock[0]) ^ ord(lock[llen - 1]) ^
														ord(lock[llen - 2]) ^ 5) + key

		# Now more tricky stuff
		key = "".join([chr(((ord(key[idx]) << 4) & 240) |
														((ord(key[idx]) >> 4) & 15))
														for idx in range(llen)])

		# print (key)
		return key

